package com.move;




import org.loon.framework.android.game.action.sprite.Sprites;
import org.loon.framework.android.game.core.geom.Point;
import org.loon.framework.android.game.core.graphics.LColor;
import org.loon.framework.android.game.core.graphics.LImage;
import org.loon.framework.android.game.core.graphics.Screen;
import org.loon.framework.android.game.core.graphics.device.LGraphics;
import org.loon.framework.android.game.core.timer.LTimerContext;





public class GameScreen extends Screen {
	
	
	
	protected LImage MapImg = null; /* 地图图片 */  
	protected Hero Ben;/*我们的主角叫做Ben吧*/
	protected int MapX;/*地图的横坐标*/
	protected Sprites sprMgr = null;  /* 精灵管理器 */ 
	protected double Suposed_Y;
	protected int vMove,hMove;/*vMove为垂直移动，hMove为水平移动*/
	protected boolean isUseful;
	
	private double RelativeX;
    private int fingerCount;/*分别为正在触屏的手指数和跳起的起始速度*/
    private float x1,y1,x2,y2;/*用以储存TouchMove事件的坐标*/   
	private int isChange;/*isHChange用以检测主角水平移动状态的改变*/
	 
	
	
	
	public GameScreen(String s) 
	{	
		/* 加载Assets目录的地图资源 */    
	 MapImg = LImage.createImage(s);
	   /*初始化主角*/ 
	 Ben = new Hero(this.getWidth(),MapImg.getWidth());  
	 /* 将精灵加入精灵管理器 */  
	 sprMgr = new Sprites();  
	 sprMgr.add(Ben.hero);     
	 
	  	 
	 x1=0;y1=0;x2=0;y2=0;fingerCount=0;
	 vMove=Hero.STATIC;hMove=Hero.STATIC;
	 isChange=Hero.STATIC;
	 MapX=0;isUseful=true;
	} 
	
	
	@Override
	public void draw(LGraphics g)
	{		
	   /*draw地图资源*/
	 g.drawImage(MapImg,MapX,0);
	 
      /*createUI函数相当于drawme*/
	 sprMgr.createUI(g); 
	 /*update函数使sprite类在规定时间画下一帧*/
	 sprMgr.update(20);
	 
	 g.setColor(LColor.black);
	 g.setFont(30);
	 g.drawString(""+Ben.hero.getY()+"  "+(Ben.hero.getX()-MapX)+"    "+(No_oneScreen.p10.x+MapX)+isUseful, 30, 100);
		
	}

		
	
	@Override
	public void alter(LTimerContext t) 
	{
     if(isUseful)
     {
      if(hMove!=isChange)/*检测到如果状态发生变化*/
	  {	 
	   Ben.walk_action(hMove,isChange);/*调用walk加载动画*/
	   isChange=hMove;
	  }	
	  
	  MapX=Ben.walk(hMove, MapX);
      RelativeX=Ben.hero.getX()-MapX; 	 
	   
	 if(vMove!=Hero.STATIC)
	   vMove=Ben.jump(vMove,Suposed_Y);
	 else 
	   Ben.hero.setY(Suposed_Y);	 
     }
 	
    
	}

	@Override
	public void onTouchDown(LTouch e)
	{ 
	 fingerCount++;
	
	 if(e.getPointer()==2&&vMove!=-1)
		 vMove=Hero.JUMP;
	} 
	
	
	
   @Override
	public void onTouchMove(LTouch e)
   { 	
	if(MoveType(x1,y1,e.getX(),e.getY(),hMove)==Hero.LEFT
			 ||MoveType(x1,y1,e.getX(),e.getY(),hMove)==Hero.RIGHT)
	     hMove=MoveType(x1,y1,e.getX(),e.getY(),hMove);
	else if(MoveType(x1,y1,e.getX(),e.getY(),vMove)==Hero.JUMP&&vMove!=-1)   	
		 vMove=MoveType(x1,y1,e.getX(),e.getY(),vMove);
	   
	 x1=e.getX();
	 y1=e.getY();
	 
   } 
    
   private int MoveType(float x,float y,float eventX,float eventY,int preMove)
   {
    float rate;
    int type = preMove;
    if(x==eventX)
	{
     if(eventY+7<y)
	    type = Hero.JUMP;
	} 	
    else
	{
	 rate=Math.abs((y-eventY)/(x-eventX));
	 if(rate>6)
	 {
	  if(eventY+7<y)
	    type = Hero.JUMP;
	  }
	  else if(rate<0.2)
	  {
	   if(eventX-7>x)
		 type = Hero.RIGHT;
	   if(eventX+7<x)
		 type = Hero.LEFT;
	  } 
	 }
	 return type;  
   }
   
   
   @Override
  	public void onTouchUp(LTouch event)
   {
	fingerCount--;
	if(fingerCount==0)
		hMove=Hero.STATIC;
	
	  
	}
   
   public boolean reachBendPoint(Point p1,Point p2)
   {
	  
    if((int)RelativeX>=p1.x
  		&&(int)RelativeX<=p2.x)
        return true;
    else
    	return false;
   }
   
   public void UporDown(Point p1,Point p2)
   {
	
	if(reachBendPoint(p1,p2))
	   Suposed_Y= countY(p1,p2,RelativeX)-Ben.hero.getHeight();  
   }
   
   
   

   private double countY(Point p1,Point p2,double x)
   {
    return (p1.y-p2.y)*(x-p1.x)/(p1.x-p2.x)+p1.y;	  
   }


  
   
   
  
}

